/*
 MOPS - Морская Открытая Пеленгационная Система
 */

package mops;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.Dialogs;
import javafx.scene.control.SplitPane;
import javafx.scene.control.SplitPaneBuilder;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPaneBuilder;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBoxBuilder;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import mops.commons.Commons;
import mops.menu.GeneralMenu;
import mops.navigator.Navigator;
import mops.toolbar.GeneralToolBar;
import mops.view.GroupObjectTable;

/**
 *
 * @author Жук Александр
 * @version 1.0
 Главный модуль системы
 */
public class MOPS extends Application {
    
    private static Stage stage;
       
    @Override
    public void start(final Stage primaryStage) {
       
        stage = primaryStage;
        
        //Обработчик закрытия окна
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
         @Override
         public void handle(WindowEvent event) {
         Dialogs.DialogResponse response = Dialogs.showConfirmDialog(stage, Commons.MENU_APPLICATION_EXIT_ACTION_TEXT,
         Commons.QUESTION_MSG, Commons.MAIN_APP_NAME, Dialogs.DialogOptions.YES_NO);
          if (response==Dialogs.DialogResponse.YES)
            stage.close();
           else
             event.consume();
          }
        });
        
        //Установка иконки приложения
        stage.getIcons().add(new Image(getClass().getResourceAsStream(Commons.APPLICATION_ICON)));
                
        try {   
    
        GeneralMenu appMenu = new GeneralMenu(stage);
        GeneralToolBar appToolBar = new GeneralToolBar();    
        
        Scene scene = SceneBuilder.create()
                .width(Commons.APP_WIDTH)
                .height(Commons.APP_HEIGHT)
                .root(
                        BorderPaneBuilder.create()
                        .top(
                                VBoxBuilder.create()
                                .children(
                                        appMenu.getMainMenu(), //Создаем главное меню
                                        appToolBar.getToolBar() //Создадим тоолбар
                                )
                                .build()
                        )
                        .center(createSplitTreeListDemoNode())
                        .bottom(addBottomPane()).maxWidth(150)
                        .build()
                   
                )
                .build();
        
        stage.setScene(scene);
        stage.setTitle(Commons.MAIN_APP_CAPTION);
        stage.show();   
        
         } catch (Exception e) {
            Dialogs.showErrorDialog(stage, e.getMessage(), Commons.STOP_ERROR, Commons.MAIN_APP_NAME);
        }        
}

    public static void main(String[] args) {
        launch(args);
    }     
    
 public FlowPane addBottomPane() {
    FlowPane flow = new FlowPane();
    flow.setPadding(new Insets(5, 0, 5, 0));
    flow.setVgap(4);
    flow.setHgap(4);
    flow.setPrefWrapLength(170); // preferred width allows for two columns
    flow.setStyle("-fx-background-color: FFFFFF;");

    ImageView pages[] = new ImageView[8];
    for (int i=0; i<8; i++) {
        pages[i] = new ImageView(
            new Image(getClass().getResourceAsStream(Commons.MENU_APPLICATION_OPTIONS_PICTURE)));
        flow.getChildren().add(pages[i]);
    }

    return flow;
}   
 
public static Stage getStage(){
    return stage;
} 
    
    
Node createSplitTreeListDemoNode() throws Exception {

Navigator appNavigator = new Navigator();
GroupObjectTable mainTable = new GroupObjectTable();

appNavigator.setTableView(mainTable.getTable());

SplitPane splitPane = SplitPaneBuilder.create()    
.items(
    appNavigator.getNavigator(),
    mainTable.getTable()
)
.build();

splitPane.setDividerPositions(0.1f,0.9f);

return splitPane;
}
    
}
